package com.example.wordmemory.utils;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wordmemory.R;
import com.example.wordmemory.entity.Word;

import java.util.List;

public class WordAdapter extends RecyclerView.Adapter<WordAdapter.ViewHolder> {
    public interface OnWordActionListener {
        void onEdit(Word word);
        void onDelete(Word word);
    }
    private List<Word> wordList;
    private OnWordActionListener listener;

    public WordAdapter(List<Word> wordList, OnWordActionListener listener) {
        this.wordList = wordList;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_word, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Word word = wordList.get(position);
        holder.tvWord.setText(word.word);
        holder.tvMeaning.setText(word.meaning);
        holder.tvExample.setText(word.example);
        holder.btnEdit.setOnClickListener(v -> listener.onEdit(word));
        holder.btnDelete.setOnClickListener(v -> listener.onDelete(word));
    }

    @Override
    public int getItemCount() {
        return wordList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvWord, tvMeaning, tvExample;
        Button btnEdit, btnDelete;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvWord = itemView.findViewById(R.id.tv_word);
            tvMeaning = itemView.findViewById(R.id.tv_meaning);
            tvExample = itemView.findViewById(R.id.tv_example);
            btnEdit = itemView.findViewById(R.id.btn_edit);
            btnDelete = itemView.findViewById(R.id.btn_delete);
        }
    }
} 